package cn.edu.zjgsu.ito.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.edu.zjgsu.ito.interceptor.TeacherControllerInterceptor;
import cn.edu.zjgsu.ito.service.ServiceFactory;
import cn.edu.zjgsu.ito.service.TeaStuService;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Record;

@Before(TeacherControllerInterceptor.class)
public class TeaStuController extends Controller {
	/*
	 * 张虎涛
	 * 2016/06/01
	 */
	private String tNo;
	private TeaStuService teaStuService =ServiceFactory.createTeaStuService();
	private List<Record> Records;
	private Record Record;
	private Record Course;
	public static final DateFormat formatter=new SimpleDateFormat("yyyy-MM-dd-HH：mm");
	
	
	public void index(){
		if(getSessionAttr("tNo")==null){
			System.out.println("登录超时，请从新登陆！");
			render("../common/errors/outlogin.html");
			return;
		}else{
			tNo=getSessionAttr("tNo");
		}
		String uuId=getPara("uuid");
		Course=teaStuService.TeaTT(uuId);
		Records=teaStuService.allStu(uuId);
        
		
		for(int i=0;i<Records.size();i++){
			Record=teaStuService.StuInf(Records.get(i).getStr("passportNo"));
			Records.get(i).set("eduStartDate", Record.get("eduStartDate"));
			Records.get(i).set("eduSeason", Record.get("eduSeason"));
		}
		setAttr("course", Course);
		setAttr("records", Records);
		setAttr("uuid", uuId);
		render("mystudent.html");
	}
	
	
	/**
	  *  导出学生Excel
	 * @throws FileNotFoundException 
	  * 
	  */
	public void TeaExportExcel() throws FileNotFoundException{
		String uuId=getPara("uuid");
        System.out.println(uuId);
		Records=teaStuService.allStu(uuId);
		

        System.out.println("-----------"+Record+" ----"+uuId);
		for(int i=0;i<Records.size();i++){
			Record=teaStuService.StuInf(Records.get(i).getStr("passportNo"));
			Records.get(i).set("eduStartDate", Record.get("eduStartDate"));
			Records.get(i).set("eduSeason", Record.get("eduSeason"));
		}
		
        Record=teaStuService.TTT(uuId);
        String courseName=Record.getStr("courseName");
		courseName=courseName.replace(" ", "-");
		Date d=new java.util.Date();
		courseName=courseName+"学生汇总表("+formatter.format(d)+").xls";
		
		
 		
 		String filedir=PathKit.getWebRootPath();
 		filedir=filedir+"\\WEB-INF\\download\\";
 		deleteFile(new File(filedir));//清空缓存区
 		OutputStream out = new FileOutputStream(filedir+courseName);
 		teaStuService.exportStuExcel(Record,Records, out,uuId);
 		String outpath=filedir+courseName;
 		
 		renderFile(new File(outpath));
	}
	
	public void deleteFile(File oldPath) {
        if (oldPath.isDirectory()) {
         File[] files = oldPath.listFiles();
         for (File file : files) {
           deleteFile(file);
         }
        }else{
          oldPath.delete();
        }
      }

}
